package com.net.action;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.googlecode.jsonplugin.annotations.JSON;
import com.net.model.Resource;
import com.net.model.Role;
import com.net.service.ResourceService;
import com.net.service.RoleService;
import com.opensymphony.xwork2.ActionSupport;

@Component("resourceAction")
@Scope("prototype")
public class ResourceAction extends ActionSupport {
	private Resource resource;
	private ResourceService resourceService;
	private RoleService roleService;
	private List<Resource> resource_list;
	private List<Resource> resource_search_list;
	private Set<Role> role_list;
	private int[] selectedList;
	private int start;
	private int limit;
	private int totalProperty;
	private boolean success;
	private String input;
	private String pw;
	public String list(){
		System.out.println(start+":"+limit+"********");
		resource_list = resourceService.getResources(start/limit, limit, null, null);
		totalProperty = resourceService.getCount();
		success = true;
		return SUCCESS;
	}
	
	public String search(){
		System.out.println(start+":"+limit+"********"+"resource name or id:" + resource.getValue());
		String str = "";
		if("".equals(resource.getValue())){
			str = "r.id="+resource.getId();
		}else if(resource.getId()==0){
			str = "r.value like '%"+resource.getValue()+"%'";
		}else {
			str = "r.value like '%"+resource.getValue()+"%'" + " or r.id="+resource.getId();
		}
		resource_list = resourceService.getResourcesByCondition(start/limit, limit, str);
		totalProperty = resourceService.getCount();
		success = true;
		return SUCCESS;
	}
	
	public String delete(){
		System.out.println("selectedLista:"+selectedList[0]+"length:"+selectedList.length);
		for(int i = 0 ; i < selectedList.length ; i++){
			resource = resourceService.loadResourceById(selectedList[i]);
			role_list = resource.getRoles();
			for(Role role : role_list){
				role.getResources().remove(resource);
				roleService.update(role);
			}
			System.out.println(resource.getValue()+"********");
			resourceService.delete(resource);
		}
		resource = null;
		role_list = null;
		success = true;
		return SUCCESS;
	}
	
	public String add(){
		HttpServletResponse response = ServletActionContext.getResponse();
		  //设置返回数据的编码为UTF-8
		  response.setCharacterEncoding("UTF-8");
		  System.out.println(resource.getValue()+":value");
		Role admin = roleService.loadRoleByName("ROLE_ADMIN");
		//System.out.println(admin.getName());
		if(admin!=null){
			admin.getResources().add(resource);
			roleService.update(admin);
			role_list = new HashSet<Role>();
			role_list.add(admin);
		}
		resource.setRoles(role_list);
		resourceService.add(resource);
		System.out.println(resource.getId()+":resource id");
		success = true;
		return SUCCESS;
	}
	
	public String update(){
		System.out.println(resource.getValue()+":value");
		System.out.println("value:"+resource.getValue());
		resourceService.update(resource);
		success = true;
		return SUCCESS;
	}
	
	public Resource getResource() {
		return resource;
	}
	public void setResource(Resource resource) {
		this.resource = resource;
	}
	@JSON(serialize=false)
	public ResourceService getResourceService() {
		return resourceService;
	}
	@javax.annotation.Resource(name="resourceService")
	public void setResourceService(ResourceService resourceService) {
		this.resourceService = resourceService;
	}
	public int getStart() {
		return start;
	}
	public void setStart(int start) {
		this.start = start;
	}
	public int getLimit() {
		return limit;
	}
	public void setLimit(int limit) {
		this.limit = limit;
	}
	public int getTotalProperty() {
		return totalProperty;
	}
	public void setTotalProperty(int totalProperty) {
		this.totalProperty = totalProperty;
	}

	public int[] getSelectedList() {
		return selectedList;
	}

	public void setSelectedList(int[] selectedList) {
		this.selectedList = selectedList;
	}

	public List<Resource> getResource_list() {
		return resource_list;
	}

	public void setResource_list(List<Resource> resource_list) {
		this.resource_list = resource_list;
	}

	public List<Resource> getResource_search_list() {
		return resource_search_list;
	}

	public void setResource_search_list(List<Resource> resource_search_list) {
		this.resource_search_list = resource_search_list;
	}

	public boolean isSuccess() {
		return success;
	}

	public void setSuccess(boolean success) {
		this.success = success;
	}

	public String getInput() {
		return input;
	}

	public void setInput(String input) {
		this.input = input;
	}

	public String getPw() {
		return pw;
	}

	public void setPw(String pw) {
		this.pw = pw;
	}
	@JSON(serialize=false)
	public RoleService getRoleService() {
		return roleService;
	}
	@javax.annotation.Resource(name="roleService")
	public void setRoleService(RoleService roleService) {
		this.roleService = roleService;
	}

	public Set<Role> getRole_list() {
		return role_list;
	}

	public void setRole_list(Set<Role> role_list) {
		this.role_list = role_list;
	}

}
